package com.ruoyi.common.function;

import com.ruoyi.common.utils.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author : hcl
 * @Date : 2023/8/10 09:58
 */
@FunctionalInterface
public interface InsertBatchFunction<T> {

    void insert(List<T> list);

    /**
     *
     */
    default void batch(int size, List<T> list) {
        if (CollectionUtils.isNotEmpty(list)) {
            List<T> smallContainer = new ArrayList<>(size);
            if (list.size() <= size) {
                insert(list);
                return;
            }
            for (int i = 0, count = 0; i < list.size(); i++, count++) {
                if (count == size) {
                    count = 0;
                    insert(smallContainer);
                    smallContainer.clear();
                }
                smallContainer.add(list.get(i));
            }
            if (CollectionUtils.isNotEmpty(smallContainer)) {
                insert(list);
                smallContainer.clear();
            }
        }
    }
}
